% autocorr2d.m
% created 03/04/2012
% Crown copyright
%
% Description: Returns autocorrelation of a matrix of real numbers.
%
% @author Kevin O'Holleran <kevin.o-holleran@npl.co.uk>
%
% @param m - matrix
% @param corrWidth - integer, scan range
%
% @return result - matrix of size (corrWidth, corrWidth)
function [ result ] = autocorr2d(m,corrWidth)
[w,h] = size(m);
result = zeros(corrWidth,corrWidth);
x0 = round(corrWidth/2);
y0 = round(corrWidth/2);
A = m(x0:(w-x0),y0:(h-y0));
for i = 1:corrWidth
    xi = round(x0-corrWidth/2+i);
    for j = 1:corrWidth
        yj = round(y0-corrWidth/2+j);
        B = m(xi:(xi+w-corrWidth),yj:(yj+h-corrWidth));
        result(i,j) = corr2d(A,B);
    end
end

end

